﻿using DotNetOpenAuth.OAuth.ChannelElements;

namespace RoketSolutions.SP2010.LinkedInWebPart.Authorization
{
    public class WebPartTokenSource:TokenSource
    {
        public WebParts.LinkedInWebPart.LinkedInWebPart LinkedInWebPart { get; set; }

        public WebPartTokenSource(string consumerKey, string consumerSecret) : base(consumerKey, consumerSecret)
        {
        }

        public override void SetToken(Token token)
        {
            if(token.TokenType == TokenType.AccessToken)
            {
                LinkedInWebPart.AccessToken = token.TokenValue;
            }
            else
            {
                LinkedInWebPart.RequestToken = token.TokenValue;
            }
            LinkedInWebPart.SaveTokens();
        }

        public override void RemoveToken(TokenType tokenType)
        {
            if(tokenType == TokenType.AccessToken)
            {
                LinkedInWebPart.AccessToken = string.Empty;
            }
            else
            {
                LinkedInWebPart.RequestToken = string.Empty;
            }
            LinkedInWebPart.SaveTokens();
        }

        public override Token GetToken(TokenType tokenType)
        {
            return CreateToken(tokenType, tokenType == TokenType.AccessToken ? LinkedInWebPart.AccessToken : LinkedInWebPart.RequestToken);
        }
    }
}
